package modelLoader;

import java.io.IOException;
import java.net.URL;

import com.jme.scene.Node;
import com.model.md5.controller.MD5Controller;
import com.model.md5.importer.MD5Importer;
import com.model.md5.interfaces.IMD5Animation;
import com.model.md5.interfaces.IMD5Node;

/** Carga los modelos utilizando MD5Importer
 * @author Sebastian Giulietti 69032 
 * @author Adrian Sanchez 64251
 *
 */
public class ModelLoader {

	static private IMD5Node MasacralityModel = null;
	static private IMD5Node AltairModel;
	static private IMD5Node BombaModel;

	static private IMD5Node BombPowerModel;
	static private IMD5Node bombUpModel;
	static private IMD5Animation idleb;
	static private IMD5Animation idlep = null;
	static private MD5Importer importer;
	static private Node playerModel;
	static private IMD5Node rugulusModel;
	static private IMD5Node siruisModel;
	static private IMD5Node SpeedPowerModel;
	static private IMD5Animation walka = null;
	static private IMD5Animation walkr = null;
	static private IMD5Animation walks = null;

	static public void LoadModels() {
		importer = new MD5Importer();
		CreatePlayerModel();
		CreateBombaModel();
		createAltairModel();
		createRugulusModel();
		CreateSiruisModel();

		createPowerUp("BombPower");
		createPowerUp("Bomb");
		createPowerUp("Speed");
		createPowerUp("Masacrality");
	}

	private static void createAltairModel() {

		String dir = "data/model/Altair/";
		URL md5mesh = ModelLoader.class.getClassLoader().getResource(
				dir + "mesh.md5mesh");
		URL animWalk = ModelLoader.class.getClassLoader().getResource(
				dir + "floating.md5anim");

		MD5Importer importer = new MD5Importer();

		IMD5Node body = null;

		try {

			importer.loadMesh(md5mesh, "ModelNode");
			body = importer.getMD5Node();

			importer.loadAnim(animWalk, "floating");
			walka = importer.getAnimation();

		} catch (Exception e) {
		}

		AltairModel = body;

	}

	private static void CreateBombaModel() {
		String dir = "data/model/bomb/";
		URL md5mesh = ModelLoader.class.getClassLoader().getResource(
				dir + "mesh.md5mesh");

		URL animIdle = ModelLoader.class.getClassLoader().getResource(
				dir + "idle.md5anim");

		IMD5Node model = null;
		idleb = null;

		try {
			importer.cleanup();

			importer.loadMesh(md5mesh, "ModelNode");
			model = importer.getMD5Node();

			importer.loadAnim(animIdle, "idle");
			idleb = importer.getAnimation();

		} catch (IOException e) {
		}

		BombaModel = model;
	}

	private static void CreatePlayerModel() {

		IMD5Node body = null;
		MD5Controller bodyController;

		String dir = "data/md5/player/";
		URL md5mesh = ModelLoader.class.getClassLoader().getResource(
				dir + "mesh.md5mesh");
		URL animIdle = ModelLoader.class.getClassLoader().getResource(
				dir + "idle.md5anim");
		URL animCaminar = ModelLoader.class.getClassLoader().getResource(
				dir + "caminar.md5anim");
		URL animPlantar = ModelLoader.class.getClassLoader().getResource(
				dir + "plantar.md5anim");
		URL animCall =ModelLoader.class.getClassLoader().getResource(
				dir + "call.md5anim");

		importer.cleanup();
		 
		IMD5Animation idle = null;
		IMD5Animation caminar = null;
		IMD5Animation plantar = null;
		IMD5Animation call = null;
		try {

			importer.loadMesh(md5mesh, "ModelNode");
			body = importer.getMD5Node();
			importer.cleanup();

			importer.loadAnim(animIdle, "idle");
			idle = importer.getAnimation();
			importer.cleanup();

			importer.loadAnim(animCaminar, "caminar");
			caminar = importer.getAnimation();
			importer.cleanup();

			importer.loadAnim(animPlantar, "plantar");
			plantar = importer.getAnimation();
			importer.cleanup();

			importer.loadAnim(animCall, "call");
			call = importer.getAnimation();

			importer.cleanup();
		} catch (IOException e) {
		}

		bodyController = new MD5Controller(body);
		bodyController.addAnimation(plantar);
		bodyController.addAnimation(call);
		bodyController.addAnimation(caminar);
		bodyController.addAnimation(idle);
		bodyController.setActive(true);
		bodyController.setRepeatType(0);
		bodyController.setSpeed(3.5f);

		body.addController(bodyController);

		playerModel = (Node) body;
	}

	private static void createPowerUp(String dir) {

		URL md5mesh = ModelLoader.class.getClassLoader().getResource(
				"data/model/pota" + dir + "/" + "mesh.md5mesh");

		URL animIdle = ModelLoader.class.getClassLoader().getResource(
				"data/model/pota" + dir + "/" + "idle.md5anim");

		importer.cleanup();
		IMD5Node body = null;

		try {

			importer.loadMesh(md5mesh, "ModelNode");
			body = importer.getMD5Node();

			importer.loadAnim(animIdle, "idle");
			idlep = importer.getAnimation();

		} catch (IOException e) {
		}

		if (dir == "BombPower")
			BombPowerModel = body;
		else if (dir == "Bomb")
			bombUpModel = body;
		else if (dir == "Speed")
			SpeedPowerModel = body;

		else if (dir == "Masacrality")
			MasacralityModel = body;

	}

	private static void createRugulusModel() {
		String dir = "data/model/Rugulus/";
		URL md5mesh = ModelLoader.class.getClassLoader().getResource(
				dir + "mesh.md5mesh");
		URL animWalk = ModelLoader.class.getClassLoader().getResource(
				dir + "floating.md5anim");

		importer.cleanup();

		IMD5Node body = null;

		try {

			importer.loadMesh(md5mesh, "ModelNode");
			body = importer.getMD5Node();

			importer.loadAnim(animWalk, "floating");
			walkr = importer.getAnimation();
		} catch (Exception e) {
		
		}

		rugulusModel = body;
	}

	private static void CreateSiruisModel() {
		String dir = "data/model/Siruis/";
		URL md5mesh = ModelLoader.class.getClassLoader().getResource(
				dir + "mesh.md5mesh");
		URL animWalk = ModelLoader.class.getClassLoader().getResource(
				dir + "caminar.md5anim");

		importer.cleanup();

		IMD5Node body = null;

		try {

			importer.loadMesh(md5mesh, "ModelNode");
			body = importer.getMD5Node();

			importer.loadAnim(animWalk, "Walk");
			walks = importer.getAnimation();

		} catch (Exception e) {
			// TODO: handle exception
		}
		siruisModel = body;
	}

	public static Node getAltairModel() {

		IMD5Node Model = AltairModel.clone();
		IMD5Animation walk = walka.clone();

		MD5Controller modelController = new MD5Controller(Model);
		modelController.addAnimation(walk);
		modelController.setActive(true);
		modelController.setRepeatType(1);
		modelController.setSpeed(1f);
		Model.addController(modelController);
		return (Node) Model;

	}

	static public Node getBombaModel() {
		IMD5Node bomb = BombaModel.clone();
		IMD5Animation idle = idleb.clone();
		MD5Controller modelController = new MD5Controller(bomb);
		modelController.addAnimation(idle);
		modelController.setActive(true);
		modelController.setRepeatType(1);
		modelController.setSpeed(1f);
		bomb.addController(modelController);
		return (Node) bomb;
	}

	static public Node getBombPowerModel() {
		IMD5Node Model = BombPowerModel.clone();
		IMD5Animation walk = idlep.clone();

		MD5Controller modelController = new MD5Controller(Model);
		modelController.addAnimation(walk);
		modelController.setActive(true);
		modelController.setRepeatType(1);
		modelController.setSpeed(1f);
		Model.addController(modelController);
		return (Node) Model;
	}

	static public Node getBombUpModel() {
		IMD5Node Model = bombUpModel.clone();
		IMD5Animation walk = idlep.clone();

		MD5Controller modelController = new MD5Controller(Model);
		modelController.addAnimation(walk);
		modelController.setActive(true);
		modelController.setRepeatType(1);
		modelController.setSpeed(1f);
		Model.addController(modelController);
		return (Node) Model;
	}

	static public MD5Controller getPlayerController() {
		return (MD5Controller) playerModel.getController(0);
	}

	static public Node getPlayerModel() {
		return playerModel;
	}

	static public Node getRugulusModel() {
		IMD5Node Model = rugulusModel.clone();
		IMD5Animation walk = walkr.clone();

		MD5Controller modelController = new MD5Controller(Model);
		modelController.addAnimation(walk);
		modelController.setActive(true);
		modelController.setRepeatType(1);
		modelController.setSpeed(1f);
		Model.addController(modelController);
		return (Node) Model;
	}

	static public Node getSiruisModel() {
		IMD5Node Model = siruisModel.clone();
		IMD5Animation walk = walks.clone();

		MD5Controller modelController = new MD5Controller(Model);
		modelController.addAnimation(walk);
		modelController.setActive(true);
		modelController.setRepeatType(1);
		modelController.setSpeed(1f);
		Model.addController(modelController);
		return (Node) Model;
	}

	static public Node getSpeedPowerModel() {
		IMD5Node Model = SpeedPowerModel.clone();
		IMD5Animation walk = idlep.clone();

		MD5Controller modelController = new MD5Controller(Model);
		modelController.addAnimation(walk);
		modelController.setActive(true);
		modelController.setRepeatType(1);
		modelController.setSpeed(1f);
		Model.addController(modelController);
		return (Node) Model;
	}
	static public Node getMasacralityModel() {
		IMD5Node Model = MasacralityModel.clone();
		IMD5Animation walk = idlep.clone();

		MD5Controller modelController = new MD5Controller(Model);
		modelController.addAnimation(walk);
		modelController.setActive(true);
		modelController.setRepeatType(1);
		modelController.setSpeed(1f);
		Model.addController(modelController);
		return (Node) Model;
	}

}
